#include"queue.h"
void QueueInit(queue* pq)
{
	assert(pq);
	pq->head = pq->tail = NULL;

}
void QueuePush(queue* pq, Newtype x)
{
	assert(pq);
	QN* newnode = (QN*)malloc(sizeof(QN));
	if (newnode == NULL)
	{
		printf("malloc,fail\n");
		exit(-1);
	}
	newnode->data = x;
	newnode->next = NULL;
	if (pq->head == NULL)
	{
		pq->head = pq->tail = newnode;
	}
	else
	{
		pq->tail->next = newnode;
		pq->tail = newnode;
	}
}
void QueuePop(queue* pq)
{
	assert(pq);
	assert(pq->head != NULL);
	QN* first = pq->head;
	pq->head = pq->head->next;
	free(first);
	first = NULL;
}
int QueueFront(queue* pq)
{
	assert(pq);
	assert(pq->head);
	return pq->head->data;
}
bool QueueEmpty(queue* pq)
{
	assert(pq);
	return pq->head;
}
int QueueSize(queue* pq)
{
	int sz = 0;
	while (QueueEmpty(pq))
	{
		sz++;
		QueuePop(pq);
	}
	return sz;
}
int QueueBack(queue* pq)
{
	QN* cur = pq->head;
	while (cur != pq->tail)
	{
		cur = cur->next;
	}
	return cur->data;
}
void QueueDestory(queue* pq)
{
	assert(pq);
	QN* cur = pq->head;
	while (cur)
	{
		QN* next = cur->next;
		free(cur);
		cur = next;
	}
	pq->head = pq->tail = NULL;
}
